package com.styletransfer.controller;

import com.styletransfer.model.ApiResponse;
import com.styletransfer.model.Combination;
import com.styletransfer.model.CombinationDetail;
import com.styletransfer.model.PageRequest;
import com.styletransfer.model.PageResponse;
import com.styletransfer.model.StyleTask;
import com.styletransfer.model.StyleTaskRequest;
import com.styletransfer.model.Work;
import com.styletransfer.model.Style;
import com.styletransfer.service.CombinationService;
import com.styletransfer.service.StyleTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 组合控制器
 *
 * 提供风格组合相关的API接口，包括创建、更新、删除、查询组合等
 */
@RestController
@RequestMapping("/combination")
public class CombinationController {

    @Autowired
    private CombinationService combinationService;

    @Autowired
    private StyleTaskService styleTaskService;

    /**
     * 创建风格组合生成任务
     *
     * @param request 包含imageUrl、styleIds和intensity的请求对象
     * @return API响应，包含任务ID
     */
    @PostMapping("/create")
    public ApiResponse<Map<String, Object>> createCombination(@RequestBody StyleTaskRequest request) {
        return combinationService.createCombination(request);
    }

    /**
     * 更新组合
     *
     * @param request 包含组合信息的请求对象
     * @return API响应
     */
    @PutMapping("/update")
    public ApiResponse<Map<String, Object>> updateCombination(@RequestBody StyleTaskRequest request) {
        return combinationService.updateCombination(request.getId(),
                                         request.getImageUrl(),
                                         request.getStyleIds(),
                                         request.getIntensity());
    }

    /**
     * 删除组合
     *
     * @param id 组合ID
     * @return API响应
     */
    @PostMapping("/delete/{id}")
    public ApiResponse<Void> deleteCombination(@PathVariable Long id) {
        return combinationService.deleteCombination(id);
    }

    /**
     * 根据ID获取组合
     *
     * @param id 组合ID
     * @return API响应
     */
    @GetMapping("/{id}")
    public ApiResponse<Combination> getCombinationById(@PathVariable Long id) {
        return combinationService.findById(id);
    }

    /**
     * 获取组合详情
     *
     * @param combinationId 组合ID
     * @return API响应
     */
    @GetMapping("/details/{combinationId}")
    public ApiResponse<List<CombinationDetail>> getCombinationDetails(@PathVariable Long combinationId) {
        return combinationService.findDetails(combinationId);
    }

    /**
     * 获取用户的组合
     *
     * @param userId 用户ID
     * @return API响应
     */
    @GetMapping("/user/{userId}")
    public ApiResponse<List<Combination>> getUserCombinations(@PathVariable Long userId) {
        return combinationService.findByUser(userId);
    }

    /**
     * 获取公开的组合
     *
     * @return API响应
     */
    @GetMapping("/public")
    public ApiResponse<List<Combination>> getPublicCombinations() {
        return combinationService.findPublic();
    }

    /**
     * 获取热门组合
     *
     * @param limit 限制数量
     * @return API响应
     */
    @GetMapping("/hot")
    public ApiResponse<List<Combination>> getHotCombinations(@RequestParam(defaultValue = "10") Integer limit) {
        return combinationService.findHot(limit);
    }

    /**
     * 获取最新组合
     *
     * @param limit 限制数量
     * @return API响应
     */
    @GetMapping("/latest")
    public ApiResponse<List<Combination>> getLatestCombinations(@RequestParam(defaultValue = "10") Integer limit) {
        return combinationService.findLatest(limit);
    }

    /**
     * 搜索组合
     *
     * @param keyword 关键词
     * @return API响应
     */
    @GetMapping("/search")
    public ApiResponse<List<Combination>> searchCombinations(@RequestParam String keyword) {
        return combinationService.search(keyword);
    }

    /**
     * 分页查询组合
     *
     * @param page 页码
     * @param size 每页大小
     * @return 分页响应
     */
    @GetMapping("/page")
    public ApiResponse<PageResponse<Combination>> getCombinationsByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        PageRequest pageRequest = new PageRequest(page, size);
        PageResponse<Combination> byPage = combinationService.findByPage(pageRequest);
        return ApiResponse.success(byPage);
    }

    /**
     * 增加组合使用次数
     *
     * @param id 组合ID
     * @return API响应
     */
    @PostMapping("/increment-use/{id}")
    public ApiResponse<Void> incrementUseCount(@PathVariable Long id) {
        return combinationService.incrementUseCount(id);
    }

    /**
     * 查询任务状态
     *
     * @param taskId 任务ID
     * @return API响应，包含任务状态
     */
    @GetMapping("/task/{taskId}")
    public ApiResponse<Map<String, Object>> getTaskStatus(@PathVariable String taskId) {
        return styleTaskService.getTaskStatus(taskId);
    }

    /**
     * 获取用户的任务列表
     *
     * @return API响应，包含任务列表
     */
    @GetMapping("/tasks")
    public ApiResponse<List<StyleTask>> getUserTasks() {
        return styleTaskService.getUserTasks();
    }

    /**
     * 获取组合相关风格
     *
     * @param id 组合ID
     * @return API响应
     */
    @GetMapping("/{id}/styles")
    public ApiResponse<List<Style>> getCombinationStyles(@PathVariable Long id) {
        return combinationService.getCombinationStyles(id);
    }

    /**
     * 获取组合示例
     *
     * @param id 组合ID
     * @return API响应
     */
    @GetMapping("/{id}/examples")
    public ApiResponse<List<Work>> getCombinationExamples(@PathVariable Long id) {
        return combinationService.getCombinationExamples(id);
    }

    /**
     * 检查组合是否已收藏
     *
     * @param id 组合ID
     * @return API响应
     */
    @GetMapping("/{id}/favorite/check")
    public ApiResponse<Boolean> checkFavoriteCombination(@PathVariable Long id) {
        return combinationService.checkFavoriteCombination(id);
    }

    /**
     * 收藏/取消收藏组合
     *
     * @param id 组合ID
     * @param request 请求体
     * @return API响应
     */
    @PostMapping("/{id}/favorite/toggle")
    public ApiResponse<Void> toggleFavoriteCombination(@PathVariable Long id, @RequestBody Map<String, Boolean> request) {
        Boolean isFavorite = request.get("favorite");
        if (isFavorite == null) {
            return ApiResponse.error("参数错误：缺少favorite字段");
        }
        return combinationService.toggleFavoriteCombination(id, isFavorite);
    }

    /**
     * 获取组合相关作品
     *
     * @param id 组合ID
     * @param pageRequest 分页请求
     * @return API响应
     */
    @GetMapping("/{id}/works")
    public ApiResponse<PageResponse<Work>> getCombinationWorks(@PathVariable Long id, PageRequest pageRequest) {
        return combinationService.getCombinationWorks(id, pageRequest);
    }
}
